
#include "StringFunctions.h"

using namespace ttg::strings;

//-----------------
//--- functions ---
//-----------------

char *ttg::strings::createCopy(const char *str)
{
	char *res = NULL;
	if (str != NULL)
	{
		size_t len = strlen(str);
		res = new char[len + 1];
		strcpy_s(res, len + 1, str);
	}
	return res;
}

char *ttg::strings::createConcatenation(const char *str1,
						  const char *str2)
{
	char *res = NULL;
	if (str1 != NULL && str2 != NULL)
	{
		size_t len = strlen(str1) + strlen(str2);
		res = new char[len + 2];
		_snprintf_s(res, len + 1, _TRUNCATE, "%s%s", str1, str2);
	}
	return res;
}

char *ttg::strings::castToWide(const char *str)
{
	if (str != NULL)
	{
		size_t len = strlen(str);
		char *res = new char[2 * len + 2];
		for (size_t i = 0; i < len + 1; i++)
		{
			res[2 * i + 1] = '\0';
			res[2 * i] = str[i];
		}
		return res;
	}
	return NULL;
}

std::string ttg::strings::clearStartEndSpaces(const char *val)
{
	if (val == NULL)
		return std::string("");
	std::string str(val);
	//Getting start.
	size_t start = 0;
	while (start < str.length() && str[start] == ' ')
		start++;
	if (start == str.length())
		return std::string("");
	//Getting count.
	size_t count = str.length() - start - 1;
	while (str[start + count] == ' ')
		count--;
	return str.substr(start, count + 1);
}
